// pages/wuye/index.js
const nt_util = require('../../utils/nt_util.js');
//let app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    limit: 10,
    notice: [],
    loadText: "点击加载更多",
    _detailHtml:'',
    hasMore:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadMore();
  },

  viewDetail: function (e) {
    nt_util.that = this; 
    let index = e.currentTarget.dataset.index
    nt_util.that.setData({
      _detailHtml: nt_util.that.data.notice[index].detail
    })
    var _queryObj = nt_util.that.data.notice[index];
    var queryObj = {
      content: _queryObj.content,
      mangeName: _queryObj.mangeName,
      mangeName: _queryObj.mangeName,
      title: _queryObj.title,
      noticeTime:_queryObj.noticeTime
    }
    var queryBean = JSON.stringify(queryObj)
    wx.navigateTo({
      url: '/pages/wuye/detail?queryBean=' + queryBean,
    })
  },

  loadMore: function () {
    var that = this;
    var params = {
      limit: this.data.limit,
      offset: (this.data.page - 1) * this.data.limit
    };
    if (that.data.hasMore) {
      nt_util.httpRequest("api/life/v1/getLifeNoticeByAppId", "post", params, function (res) {
        var noticePage = res.data.noticePage;
        var currentPage = that.data.page; 
        var newNotice = that.data.notice; 
        that.setData({
          page: currentPage + 1,                    // 页码自增
          notice: newNotice.concat(noticePage.rows) // 追加新记录
        });
        if (that.data.notice.length >= noticePage.total) {
          that.setData({
            loadText: "已经加载全部",
            hasMore:false
          });
        } else {
          that.setData({
            loadText: "点击加载更多"
          });
        }
      });      
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }

})